package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.IOUtil;
import com.ruoyi.system.domain.OblistDetail;
import com.ruoyi.system.service.IOblistDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OutboundList;
import com.ruoyi.system.service.IOutboundListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库单Controller
 * 
 * @author ruoyi
 * @date 2025-09-02
 */
@RestController
@RequestMapping("/system/outboundList")
public class OutboundListController extends BaseController
{
    @Autowired
    private IOutboundListService outboundListService;
    @Autowired
    private IOblistDetailService oblistDetailService;


    /**
     * 修改出货单商品信息
     * @param data
     * @return
     */
    @RequestMapping("/updateOrderGoods")
    public AjaxResult updateOrderGoods(@RequestBody JSONObject data)
    {
        System.out.println("更新出货单商品信息："+data.toString());
        long listid = data.getLong("listid");
        List<OblistDetail> oblistDetails = JSON.parseArray(data.getString("goods"), OblistDetail.class);

        AjaxResult ajaxResult = outboundListService.updateOrderGoods(listid, oblistDetails);
        return ajaxResult;
    }

    /**
     * 获取出库单的所有商品
     * @param id
     * @return
     */
    @RequestMapping("/listGoods")
    public TableDataInfo listGoods(Long id)
    {
        OblistDetail oblistDetail = new OblistDetail();
        oblistDetail.setListid(id);
        List<OblistDetail> list = oblistDetailService.selectOblistDetailList(oblistDetail);


        return getDataTable(list);
    }
    /**
     * 查询出库单列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping("/list")
    public TableDataInfo list(OutboundList outboundList)
    {
        startPage();
        List<OutboundList> list = outboundListService.selectOutboundListList(outboundList);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OutboundList outboundList)
    {
        List<OutboundList> list = outboundListService.selectOutboundListList(outboundList);
        ExcelUtil<OutboundList> util = new ExcelUtil<OutboundList>(OutboundList.class);
        util.exportExcel(response, list, "出库单数据");
    }

    /**
     * 获取出库单详细信息
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(outboundListService.selectOutboundListById(id));
    }

    /**
     * 审核出库单
     * @param id
     * @return
     */
    @RequestMapping("/reviewList")
    public AjaxResult reviewList(@RequestParam("id")long id)
    {
        AjaxResult ajaxResult = outboundListService.reviewList(id);
        return ajaxResult;
    }

    /**
     * 新增出库单
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JSONObject data)
    {
        //补齐出库单信息
        data.put("orderNo", "CK"+IOUtil.getId(4));
        data.put("userId",getUserId());
        data.put("user",getUsername());
        return toAjax(outboundListService.insertOutboundList(data));
    }

    /**
     * 修改出库单
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OutboundList outboundList)
    {
        return toAjax(outboundListService.updateOutboundList(outboundList));
    }

    /**
     * 删除出库单
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(outboundListService.deleteOutboundListByIds(ids));
    }
}
